package com.zefor.warns;

import com.grapecity.documents.excel.Q;
import com.zefor.utils.CacheUtil;
import kd.bos.cache.LocalMemoryCache;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.earlywarn.EarlyWarnContext;
import kd.bos.entity.earlywarn.warn.plugin.IEarlyWarnWriteOut;
import kd.bos.kdtx.common.exception.KdtxException;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.script.jsengine.debug.KDebugLog;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.drp.mdr.common.util.ObjectUtil;

import java.math.BigDecimal;


/**
 * 自定义数据源写出
 */
public class EleWarnWriteOutPlugin implements IEarlyWarnWriteOut {
    //获取缓存操作
    private final LocalMemoryCache localCache = CacheUtil.getCache();

    @Override
    public void writeOut(DynamicObjectCollection dynamicObjectCollection, EarlyWarnContext earlyWarnContext) {
        //设置预警类型
        localCache.put("WarnType", "用电");
        //查询用电预警标准值
        DynamicObject eleWarnStandard = BusinessDataServiceHelper.load("n716_warnstandard", "n716_mulilangtextfield, n716_decimalfield, n716_decimalfield1", new QFilter("n716_combofield", QCP.like, "用电量").toArray())[0];
        //最小值
        double minNum = eleWarnStandard.getBigDecimal("n716_decimalfield").doubleValue();
        //最大值
        double maxNum = eleWarnStandard.getBigDecimal("n716_decimalfield1").doubleValue();
        //过滤后的条件
        for (int i = 0; i < dynamicObjectCollection.size(); i++) {
            DynamicObjectCollection entryentity = dynamicObjectCollection.get(i).getDynamicObjectCollection("n716_entryentity");
            for (int j = 0; j < entryentity.size(); j++) {
                BigDecimal item = queryCorralEleUseByEleObj(dynamicObjectCollection.get(i), entryentity.get(j));
                if (ObjectUtil.isEmpty(item)) {
                    throw new KdtxException("未获取到当前栏舍用电量");
                }

                //在预警标准之内,移除该项
                if (item.doubleValue() >= minNum && item.doubleValue() <= maxNum) {
                    //触发预警条件,移除该项
                    entryentity.remove(entryentity.get(j));
                    j--;
                } else if (item.doubleValue() < minNum) {
                    //未触发预警条件,设置通知消息
                    localCache.put(entryentity.get(j).getPkValue() + "warnMsg", "用电量小于预设预警值");
                } else {
                    localCache.put(entryentity.get(j).getPkValue() + "warnMsg", "用电量大于预设预警值");
                }
            }
            //如果所有项都不符合预警条件
            if (entryentity.size() == 0) {
                //那么把该项从中移除
                dynamicObjectCollection.remove(dynamicObjectCollection.get(i));
                i--;
            }
        }
    }


    /*
     * 获取当前栏舍的用电量
     * */
    private BigDecimal queryCorralEleUseByEleObj(DynamicObject eleObj, DynamicObject dynamicObject) {
        DynamicObjectCollection entryentity = BusinessDataServiceHelper.loadSingle(eleObj.getPkValue(), "n716_ele_info").getDynamicObjectCollection("n716_entryentity");
        for (DynamicObject object : entryentity) {
            if (object.getLong("id") == dynamicObject.getLong("id")) {
                return object.getBigDecimal("n716_tm");
            }
        }
        return null;
    }
}
